package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.datastore.Text;

import data.Data;

import model.Domain;

public class Insert {
	public Insert(Domain domain)
	{
		URL url;
		try {
			url = new URL("http://67.205.109.33:8080/workeranalytic/put?name="+domain.getName());
		
	    	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setReadTimeout(500000);
			connection.setConnectTimeout(1000000);
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("utf-8")));
			String content = "";
	       	while(1==1)
			{
					 String str = reader.readLine();
					 if(str==null) break;
					 content+=str;   				
			}
	       	Document doc = Jsoup.parse(content);
	       	
			String wotTrust = doc.select("#wotTrust").html();
			String wotVendor = doc.select("#wotVendor").html();
			String wotPrivacy = doc.select("#wotPrivacy").html();
			String wotChild = doc.select("#wotChild").html();
			String alexa = doc.select("#alexa").html();
			String google = doc.select("#google").html();
			String ip = doc.select("#ip").html();
			String speedSite = doc.select("#speedSite").html();
			String ageDomain = doc.select("#ageDomain").html();
			String charset = doc.select("#charset").html();
			String language = doc.select("#language").html();
			String server = doc.select("#server").html();
			String jsLib = doc.select("#jsLib").html();
			String outlink = doc.select("#outlink").html();
			String whois = doc.select("#whois").html();
			String whoisIp = doc.select("#whoisIp").html();
			String title = doc.select("#title").html();
			String description = doc.select("#description").html();
			String keyword = doc.select("#keyword").html();
			String domain1 = doc.select("#domain").html();
			String adsend = doc.select("#adsend").html();
			String analytics = doc.select("#analytics").html();
			String visit = doc.select("#visit").html();
			alexa = alexa.replaceAll("[^0-9]+", "");
			if(alexa.equals(""))
			{
				alexa="0";
			}
	       	
			PersistenceManager psm = Data.get().getPersistenceManager();
			
			
			int ran = new Random().nextInt(1000000);
        	if(!alexa.equals("0"))
        	{
        		ran = Integer.parseInt(alexa);
        	}
        	Query query = psm.newQuery(Domain.class);
        	query.setFilter("alexa < "+ran);
        	query.setOrdering("alexa desc");
        	query.setRange(0, 30);

        	List<Domain> domainList=  (List<Domain>)query.execute();
        	ArrayList<String> nearSite = new ArrayList<String> ();
        	if(domainList.size() > 0)
        	{
        		for (int i = 0; i < domainList.size(); i++) {
        			nearSite.add(domainList.get(i).getName());
				}
        	}
			
	       	domain.setNearSite(nearSite);
			domain.setWotTrust(new Text(wotTrust));
			domain.setWotVendor(new Text(wotVendor));
			domain.setWotPrivacy(new Text(wotPrivacy));
			domain.setWotChild(new Text(wotChild));
			domain.setAlexa(Integer.valueOf(alexa));
			domain.setGoogle(new Text(google));
			domain.setIp(ip);
			domain.setSpeedSite(new Text(speedSite));
			domain.setAgeDomain(new Text(ageDomain));
			domain.setCharset(new Text(charset));
			domain.setLanguage(new Text(language));
			domain.setServer(new Text(server));
			domain.setJsLib(new Text(jsLib));
			domain.setOutlink(new Text(outlink));
			domain.setWhois(new Text(whois));
			domain.setWhoisIp(new Text(whoisIp));
			domain.setTitle(new Text(title));
			domain.setDescription(new Text(description));
			domain.setKeyword(new Text(keyword));
			domain.setDomain(domain1);
			domain.setAdsend(adsend);
			domain.setAnalytics(analytics);
			domain.setVisit(new Text(visit));
			psm.makePersistent(domain);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
